package com.myapp.ruyixing.ui;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.CityHotMenu;
import com.myapp.ruyixing.model.CityHotMenu.CityHotLabel;
import com.myapp.ruyixing.model.CityHotMenu.HotCell;
import com.myapp.ruyixing.ui.widget.TitleBar;

public class SearchCityDistrictFragment extends Fragment implements
		OnClickListener {

	private ListView mCategoryListView;
	private MyListAdapter mListAdapter;
	private LinearLayout mLabelContainer;

	private List<CityHotLabel> mAllCatetoriesData;
	private List<CityHotMenu.HotCell> mCategoriesData;
	private String currentLabel;
	private View currentHighlightLabelButton;

	private boolean isShowTitleBar = true;

	private OnDistrictItemClickListener mOnItemClickListener;

	public static interface OnDistrictItemClickListener {
		public void onDistrictItemClicked(HotCell cell);
	}

	private RYHttpResponseHandler mHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			CityHotMenu cityHotMenu = (CityHotMenu) o;

			mAllCatetoriesData.clear();
			mAllCatetoriesData.addAll(cityHotMenu.labels);
			initLabels();
			if (mAllCatetoriesData.size() > 0) {
				currentLabel = mAllCatetoriesData.get(0).label;
				currentHighlightLabelButton = mLabelContainer.getChildAt(0);
				setCurrentLabel(currentLabel);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			if (getActivity() != null) {
				Toast.makeText(getActivity().getApplicationContext(), "服务器异常，请稍后再试", 
						Toast.LENGTH_SHORT).show();
			}
		}
	};

	public static SearchCityDistrictFragment getInstance(boolean showTitleBar) {
		SearchCityDistrictFragment fragment = new SearchCityDistrictFragment();
		Bundle bundle = new Bundle();
		bundle.putBoolean("showTitleBar", showTitleBar);
		fragment.setArguments(bundle);

		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (getArguments() != null) {
			isShowTitleBar = getArguments().getBoolean("showTitleBar", true);
		}
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_search_city_district, container,
				false);
	};

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {

		mAllCatetoriesData = new ArrayList<CityHotLabel>();
		mCategoriesData = new ArrayList<CityHotMenu.HotCell>();
		initViews(view);

		mListAdapter = new MyListAdapter(mCategoriesData);
		mCategoryListView.setAdapter(mListAdapter);

		RYHttpClient.getCityHotDistrict(getActivity(), mHttpResponseHandler);
		
		if (!isShowTitleBar) {
			view.findViewById(R.id.title_bar).setVisibility(View.GONE);
		}
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		if (getActivity() instanceof OnDistrictItemClickListener) {
			mOnItemClickListener = (OnDistrictItemClickListener) getActivity();
		}
	}

	private void initViews(View view) {
		mCategoryListView = (ListView) view.findViewById(R.id.categories);
		mLabelContainer = (LinearLayout) view.findViewById(R.id.label_container);

		((TitleBar) view.findViewById(R.id.title_bar))
				.setOnIconClickedListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.img_icon:
			getActivity().finish();
			break;

		default:
			if (v instanceof Button) {
				if (currentLabel.equals(((Button) v).getText().toString())) {
					return;
				}

				currentHighlightLabelButton.setSelected(false);
				currentHighlightLabelButton = v;
				setCurrentLabel(((Button) v).getText().toString());
			}
		}
	}

	private void initLabels() {
		for (CityHotLabel label : mAllCatetoriesData) {
			Button button = (Button) LayoutInflater.from(getActivity()).inflate(
					R.layout.button_search_city_district, mLabelContainer,
					false);
			button.setText(label.label);
			button.setOnClickListener(this);
			mLabelContainer.addView(button);
		}
	}

	private void setCurrentLabel(String label) {
		currentLabel = label;
		currentHighlightLabelButton.setSelected(true);

		mCategoriesData.clear();
		for (CityHotLabel item : mAllCatetoriesData) {
			if (currentLabel.equals(item.label)) {
				mCategoriesData.addAll(item.cells);
			}
		}

		mListAdapter.notifyDataSetChanged();
	}

	private class MyListAdapter extends ArrayAdapter<CityHotMenu.HotCell> {
		public MyListAdapter(List<CityHotMenu.HotCell> data) {
			super(getActivity(), -1, data);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(
						R.layout.cell_search_city_menu, parent, false);
			}

			((TextView) convertView).setText(getItem(position).name);
			((TextView) convertView).setTag(getItem(position));
			convertView.setOnClickListener(mDistrictViewClickListener);

			return convertView;
		}
	}

	private OnClickListener mDistrictViewClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			HotCell cell = (HotCell) v.getTag();
			if (cell != null && mOnItemClickListener != null) {
				mOnItemClickListener.onDistrictItemClicked(cell);
			}
		}
	};
}
